#include "SynergyProxyModel.h"



#include <ItemModel.h>
#include <ItemReference.h>



#include <QDebug>



namespace
{
QString SKILL_GROUP_ID = ".Skill";
}


SynergyProxyModel::SynergyProxyModel(QObject *parent) :
    ReferenceProxyModel(parent)
{
}



bool SynergyProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    qDebug() << Q_FUNC_INFO;
    if (role != Qt::CheckStateRole) return false;

    qDebug("source target check");
    if (!m_sourceTarget.isValid()) return false;

    qDebug("source index check");
    SourceIndex sourceIndex = mapToSource(index);
    if (!sourceIndex.isValid()) return false;

    qDebug("id index check");
    QModelIndex idIndex = sourceIndex.sibling(sourceIndex.row(), AbstractItem::IdData);
    if (!idIndex.isValid()) return false;

    ItemModel* model = dynamic_cast<ItemModel*>(sourceModel());
    if (!model || m_sourceTarget.model() != model) return false;

    qDebug("create or remove reference");
    if (!index.data(Qt::CheckStateRole).toBool())
    {
        qDebug("Setting reference!");

        // reference does not exist, add reference.
        if (!model->insertRow(0, m_sourceTarget))
        {
            qDebug("Failed to add row!");
            return false;
        }

        QModelIndex refIndex = model->index(0, ItemReference::RefData, m_sourceTarget);
        QModelIndex typeIndex = refIndex.sibling(refIndex.row(), ItemReference::TypeData);
        if (!refIndex.isValid() || !typeIndex.isValid()) return false;

        model->setData(refIndex, idIndex.data(Qt::EditRole), Qt::EditRole);
        model->setData(typeIndex, ItemReference::SynergyRef, Qt::EditRole);
    }
    else
    {
        qDebug("Removing reference");
        QModelIndex refIindex = model->index(0, ItemReference::RefData, m_sourceTarget);
        while (refIindex.isValid())
        {
            if (refIindex.data(Qt::EditRole).toString() != idIndex.data(Qt::EditRole)) continue;

            if (!model->removeRow(refIindex.row(), m_sourceTarget))
            {
                qDebug("Failed to remove row!");
                return false;
            }
            break;
        }
    }

    emit dataChanged(index, index);
    return true;
}



Qt::ItemFlags SynergyProxyModel::flags(const QModelIndex &index) const
{
    Q_UNUSED(index);
    return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
}



void SynergyProxyModel::remap()
{
    clear();

    ItemModel* model = dynamic_cast<ItemModel*>(sourceModel());
    if (!model) return;

    QModelIndex sourceGroupName = model->index(0,AbstractItem::NameData, QModelIndex());
    while (sourceGroupName.isValid())
    {
        QModelIndex sourceGroupId = sourceGroupName.sibling(sourceGroupName.row(), AbstractItem::IdData);
        if (!sourceGroupId.data(Qt::EditRole).toString().contains(SKILL_GROUP_ID))
        {
            sourceGroupName = sourceGroupName.sibling(sourceGroupName.row()+1, AbstractItem::NameData);
            continue;
        }

        QModelIndex sourceItemName = sourceGroupName.child(0, AbstractItem::NameData);
        while (sourceItemName.isValid())
        {
            ProxyIndex proxyItemName = createIndex(sourceItemName.row(), NameField, sourceItemName.internalPointer());
            addMapping(sourceItemName, proxyItemName);

            sourceItemName = sourceItemName.sibling(sourceItemName.row()+1, AbstractItem::NameData);
        }

        break;
    }
}
